package com.guangzhi.weijianzhi.company.manager;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.entity.PublishPositionEntity;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.utils.Misc;

public class PayPassWrodActivity extends BaseActivity implements OnClickListener {

	private EditText mPay;
	private EditText mPay2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tz_pay_password_layout);
		
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		goBack.setOnClickListener(this);
		titleTextV.setText("设置支付密码");
		mPay = (EditText) findViewById(R.id.pay_paw);
		mPay2 = (EditText) findViewById(R.id.pay_paw_two);
		findViewById(R.id.next).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	    switch (v.getId()) {
		case R.id.next:
			if(isOk()){
				setPassword();
			}
			break;
		case R.id.title_back:
			onBackPressed();
			break;

		default:
			break;
		}	
	}
	
	private boolean isOk(){
		
		if(!Misc.notNull(mPay.getText().toString())){
            Misc.alert("请输入支付密码");
            return false;
		}else if(mPay.getText().toString().length()<6||mPay.getText().toString().length()>12){
			Misc.alert("支付密码6-12位");
			return false;
		}
		
		else if(!Misc.notNull(mPay2.getText().toString())){
            Misc.alert("请再次输入支付密码");	
            return false;
		}else if(!mPay.getText().toString().equals(mPay2.getText().toString())){
			Misc.alert("两次密码不一致");
			return false;
		}else{
			return true;
		}
		
	}
	
	private void setPassword(){
		HttpRequestUtils.doHttpSetPassword(mPay.getText().toString(), mPay2.getText().toString(), new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					if(jsonO.optBoolean("status")){
						JSONArray optJSONArray = jsonO.optJSONArray("data");
						
						if(optJSONArray.length()!=0){
							JSONObject optJSONObject = optJSONArray.optJSONObject(0);
							
							Misc.alert(optJSONObject.optString("result"));
							PublishPositionEntity.hasPayPassword = true;
							finish();
						}
						
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}
